

# analyze conjoint. main specification is of the form 
# Y_it = a_i + b D_it + e_it
# where a_i is a respondent fixed effect and D is the 
# treatment vector. The coefs b give the AMCEs.
# Cluster standard errors at respondent level.



# will's helper package (has cluster SE's)
if (!require(wpmarble)){
  remotes::install_github("wpmarble/wpmarble")
  stopifnot(require(wpmarble))
}
library(sandwich)
library(ggplot2)
library(dplyr)
library(forcats)
library(ggthemes)
library(estimatr)
library(stargazer)
library(xtable)


conjoint = readRDS("Modified Data/Clean_reshaped.RDS")


## make text bigger!
theme_set(theme_bw() + 
            theme(panel.spacing = unit(0, "in"), 
                  panel.grid = element_blank(),
                  text = element_text(size = 14))
)


# main regression specifications for both outcomes 

# outcome variable - answering at least "moderately interested"
# continuous outcome, rescaled so equally spaced from 0 to 1 
conjoint = conjoint %>% 
  mutate(interest_bin = as.numeric(interest > 0),
         interest_num = interest - min(interest,na.rm=TRUE)) %>% 
  mutate(interest_num = interest_num / max(interest_num,na.rm=TRUE))


interest_formula = formula(interest_num ~ salary + fundraising + advertising + opp_exp + opp_ideol + factor(responseid))
prob_formula = formula(winprob ~ salary + fundraising + advertising + opp_exp + opp_ideol + factor(responseid))



# FULL SAMPLE ANALYSIS ----------------------------------------------------


refcats = tibble(variable = c("salary$15,000", "fundraising$25,000", 
                                  "advertisingNo negative ads", 
                                  "opp_expNo prior experience",
                                  "opp_ideolModerate"),
                     coef = 0, se = 0) 


## outcome standard deviations: 
# overall SD
overall_sd = sd(conjoint$interest_num, na.rm=T) 

# SD after respondent FE -- i.e. within-respondent variation
within_sd = sd(lm(interest_num ~ factor(responseid), conjoint)$residuals)


# what proportion of overall SD is within-respondent variation?
within_sd / overall_sd


# correlation between outcom measures
with(conjoint, cor(interest_num, winprob, use = "pair", method = "spear"))

# outcome: interest -------------------------------------------------------


interest_reg = lm(interest_formula, conjoint)
interest_cov = vcovCluster(interest_reg, conjoint$responseid)
interest_se = sqrt(diag(interest_cov))
out_df_interest = data_frame(variable = names(coef(interest_reg)[2:10]),
                             coef = coef(interest_reg)[2:10],
                             se = interest_se[2:10])
out_df_interest = rbind(out_df_interest, refcats)
out_df_interest$attribute = NA_character_
out_df_interest$attribute[grepl("salary", out_df_interest$variable)] = "Salary"
out_df_interest$attribute[grepl("fundraising", out_df_interest$variable)] = "Fundraising"
out_df_interest$attribute[grepl("advertising", out_df_interest$variable)] = "Advertising"
out_df_interest$attribute[grepl("opp_exp", out_df_interest$variable)] = "Opponent's Experience"
out_df_interest$attribute[grepl("opp_ideol", out_df_interest$variable)] = "Opponent's Ideology"

out_df_interest$variable = gsub("salary|fundraising|advertising|opp_exp|opp_ideol", "", out_df_interest$variable)

out_df_interest$variable = factor(out_df_interest$variable, 
                                  c("$15,000", "$50,000", "$80,000",  # salary
                                    "$25,000", "$100,000", "$300,000", # fundraising
                                    "No negative ads", "Mostly negative ads", # advertising
                                    "No prior experience", "Mayor", "Incumbent", # opponent experience
                                    "Moderate", "Somewhat ideological", "Very ideological")) # opponent ideol
out_df_interest$attribute = factor(out_df_interest$attribute, 
                                   c("Salary", "Fundraising", "Advertising", 
                                     "Opponent's Experience", "Opponent's Ideology"))

ggplot(out_df_interest, aes(y = fct_rev(variable), x = coef, 
                            xmin = coef - 1.96*se, xmax = coef + 1.96*se)) + 
  geom_vline(xintercept = 0, lty = 3) + 
  geom_errorbarh(height = 0) + 
  geom_point() + 
  facet_wrap(~attribute, ncol = 1, scales = "free_y") + 
  labs(x = "Coefficient", y = NULL) +
  # ggtitle("Effect on Interest in Running") +
  scale_x_continuous(breaks = seq(-.2, .2, .05)) +
  coord_cartesian(xlim = c(-.17, .17))
ggsave("figs/interest_conjoint.pdf", width=6*.75, height=8*.75)
ggsave("figs/interest_conjoint.png", width=6*.75, height=8*.75, units="in")


# Regression coefs
summary(interest_reg)$coefficients[1:10,]

# outcome in terms of within-person SD in the outcome variable
interest_reg$coefficients[1:10] / within_sd

# outcome in terms of overall SD in the outcome variable
interest_reg$coefficients[1:10] / overall_sd




# outcome: win probability ------------------------------------------------


prob_reg = lm(prob_formula, conjoint)
prob_cov = vcovCluster(prob_reg, conjoint$responseid)
prob_se = sqrt(diag(prob_cov))
out_df_prob = data_frame(variable = names(coef(prob_reg)[2:10]),
                         coef = coef(prob_reg)[2:10],
                         se = prob_se[2:10])
out_df_prob = rbind(out_df_prob, refcats)
out_df_prob$attribute = NA_character_
out_df_prob$attribute[grepl("salary", out_df_prob$variable)] = "Salary"
out_df_prob$attribute[grepl("fundraising", out_df_prob$variable)] = "Fundraising"
out_df_prob$attribute[grepl("advertising", out_df_prob$variable)] = "Advertising"
out_df_prob$attribute[grepl("opp_exp", out_df_prob$variable)] = "Opponent's Experience"
out_df_prob$attribute[grepl("opp_ideol", out_df_prob$variable)] = "Opponent's Ideology"

out_df_prob$variable = gsub("salary|fundraising|advertising|opp_exp|opp_ideol", "", out_df_prob$variable)

out_df_prob$variable = factor(out_df_prob$variable, 
                              c("$15,000", "$50,000", "$80,000",  # salary
                                "$25,000", "$100,000", "$300,000", # fundraising
                                "No negative ads", "Mostly negative ads", # advertising
                                "No prior experience", "Mayor", "Incumbent", # opponent experience
                                "Moderate", "Somewhat ideological", "Very ideological")) # opponent ideol
out_df_prob$attribute = factor(out_df_prob$attribute, 
                               c("Salary", "Fundraising", "Advertising", 
                                 "Opponent's Experience", "Opponent's Ideology"))

ggplot(out_df_prob, aes(y = fct_rev(variable), x = coef, 
                        xmin = coef - 1.96*se, xmax = coef + 1.96*se)) + 
  geom_vline(xintercept = 0, lty = 3) + 
  geom_errorbarh(height = 0) + 
  geom_point() + 
  facet_wrap(~attribute, ncol = 1, scales = "free_y") + 
  labs(x = "Coefficient", y = NULL) +
  scale_x_continuous(breaks = seq(-15, 10, 5)) + 
  coord_cartesian(xlim = c(-16, 11))
# ggtitle("Effect on Self-Assessed Win Probability") 
ggsave("figs/prob_conjoint.pdf", width=6*.75, height=8*.75)
ggsave("figs/prob_conjoint.png", width=6*.75, height=8*.75, units="in")






stargazer(list(interest_reg, prob_reg), se = list(interest_se, prob_se), 
          keep = "salary|advertising|fundraising|opp",
          align = TRUE, float = FALSE, style = "ajps",
          omit.stat = c("adj.rsq","F", "SER"),
          type = "latex")





# Investigate implausible salary/fundraising categories --------------------

# Respondents may be confused by implausible combinations of fundraising
# and salary -- e.g. $80k salary and $25k fundraising. Take two approaches
# to seeing how much this matters. First, estimate models that interact
# levels of salary + fundraising. Results show no significant interactions
# and point estimates that are largely similar. Next, plot marginal means
# for each salary-fundraising condition. 

# Include interactions between them 
nointer_reg = lm(interest_num ~ salary + fundraising + advertising + opp_exp + 
                      opp_ideol + factor(responseid), conjoint)
nointer_cov = vcovCluster(nointer_reg, conjoint$responseid)

inter_reg   = lm(interest_num ~ salary * fundraising + advertising + opp_exp + 
                   opp_ideol + factor(responseid), conjoint)
inter_cov   = vcovCluster(inter_reg, conjoint$responseid)

waldtest(inter_reg, nointer_reg, vcov = inter_cov)

# make coef plot 
tmp = expand.grid(salary = unique(conjoint$salary), 
                  fundraising = unique(conjoint$fundraising)) 
tmp = na.omit(tmp)

tmp$est = NA
tmp$var  = NA
tmp$est[tmp$salary == "$15,000" & tmp$fundraising == "$25,000"] = 0
tmp$var[tmp$salary == "$15,000" & tmp$fundraising == "$25,000"] = 0

tmp$est[tmp$salary == "$50,000" & tmp$fundraising == "$25,000"] = coef(inter_reg)["salary$50,000"]
tmp$var[tmp$salary == "$50,000" & tmp$fundraising == "$25,000"] = inter_cov["salary$50,000","salary$50,000"]

tmp$est[tmp$salary == "$80,000" & tmp$fundraising == "$25,000"] = coef(inter_reg)["salary$80,000"]
tmp$var[tmp$salary == "$80,000" & tmp$fundraising == "$25,000"] = inter_cov["salary$80,000","salary$80,000"]

tmp$est[tmp$salary == "$15,000" & tmp$fundraising == "$100,000"] = coef(inter_reg)["fundraising$100,000"]
tmp$var[tmp$salary == "$15,000" & tmp$fundraising == "$100,000"] = inter_cov["fundraising$100,000","fundraising$100,000"]

tmp$est[tmp$salary == "$50,000" & tmp$fundraising == "$100,000"] = coef(inter_reg)["salary$50,000"] + coef(inter_reg)["fundraising$100,000"] + coef(inter_reg)["salary$50,000:fundraising$100,000"]
tmp$var[tmp$salary == "$50,000" & tmp$fundraising == "$100,000"] = inter_cov["salary$50,000","salary$50,000"] + inter_cov["fundraising$100,000","fundraising$100,000"] + 2 * inter_cov["salary$50,000","fundraising$100,000"]

tmp$est[tmp$salary == "$80,000" & tmp$fundraising == "$100,000"] =  coef(inter_reg)["salary$80,000"] + coef(inter_reg)["fundraising$100,000"] + coef(inter_reg)["salary$80,000:fundraising$100,000"]
tmp$var[tmp$salary == "$80,000" & tmp$fundraising == "$100,000"] =  inter_cov["salary$80,000","salary$80,000"] + inter_cov["fundraising$100,000","fundraising$100,000"] + 2 * inter_cov["salary$80,000","fundraising$100,000"]

tmp$est[tmp$salary == "$15,000" & tmp$fundraising == "$300,000"] = coef(inter_reg)["fundraising$300,000"]
tmp$var[tmp$salary == "$15,000" & tmp$fundraising == "$300,000"] = inter_cov["fundraising$300,000","fundraising$300,000"]

tmp$est[tmp$salary == "$50,000" & tmp$fundraising == "$300,000"] = coef(inter_reg)["salary$50,000"] + coef(inter_reg)["fundraising$300,000"] + coef(inter_reg)["salary$50,000:fundraising$300,000"]
tmp$var[tmp$salary == "$50,000" & tmp$fundraising == "$300,000"] = inter_cov["salary$50,000","salary$50,000"] + inter_cov["fundraising$300,000","fundraising$300,000"] + 2 * inter_cov["salary$50,000","fundraising$100,000"]

tmp$est[tmp$salary == "$80,000" & tmp$fundraising == "$300,000"] =  coef(inter_reg)["salary$80,000"] + coef(inter_reg)["fundraising$300,000"] + coef(inter_reg)["salary$80,000:fundraising$300,000"]
tmp$var[tmp$salary == "$80,000" & tmp$fundraising == "$300,000"] =  inter_cov["salary$80,000","salary$80,000"] + inter_cov["fundraising$300,000","fundraising$300,000"] + 2 * inter_cov["salary$80,000","fundraising$100,000"]

tmp$se = sqrt(tmp$var)

ggplot(tmp) + 
  aes(x = est, xmin = est - 1.96*se, xmax = est + 1.96*se, y = fundraising, colour = salary) + 
  geom_vline(xintercept = 0, lty = 3) + 
  geom_errorbarh(position = position_dodgev(height=.25), height = 0) + 
  geom_point(position = position_dodgev(height=.25)) + 
  labs(x = "AMCE on Interest Relative to a\nLow-Salary, Low-Fundraising Race", y = "Fundraising") + 
  scale_colour_grey(name = NULL) + 
  guides(colour = guide_legend(rev=TRUE))
ggsave("figs/interest_conjoint_salfund_interact.pdf",width=6,height=4)


# Include interactions between them 
nointer_reg2 = lm(winprob ~ salary + fundraising + advertising + opp_exp + 
                    opp_ideol + factor(responseid), conjoint)
nointer_cov2 = vcovCluster(nointer_reg2, conjoint$responseid)

inter_reg2   = lm(winprob ~ salary * fundraising + advertising + opp_exp + 
                    opp_ideol + factor(responseid), conjoint)
inter_cov2   = vcovCluster(inter_reg2, conjoint$responseid)

waldtest(inter_reg2, nointer_reg2, vcov = inter_cov2)

# make coef plot 
tmp2 = expand.grid(salary = unique(conjoint$salary), 
                   fundraising = unique(conjoint$fundraising)) 
tmp2 = na.omit(tmp2)

tmp2$est = NA
tmp2$var  = NA
tmp2$est[tmp2$salary == "$15,000" & tmp2$fundraising == "$25,000"] = 0
tmp2$var[tmp2$salary == "$15,000" & tmp2$fundraising == "$25,000"] = 0

tmp2$est[tmp2$salary == "$50,000" & tmp2$fundraising == "$25,000"] = coef(inter_reg2)["salary$50,000"]
tmp2$var[tmp2$salary == "$50,000" & tmp2$fundraising == "$25,000"] = inter_cov2["salary$50,000","salary$50,000"]

tmp2$est[tmp2$salary == "$80,000" & tmp2$fundraising == "$25,000"] = coef(inter_reg2)["salary$80,000"]
tmp2$var[tmp2$salary == "$80,000" & tmp2$fundraising == "$25,000"] = inter_cov2["salary$80,000","salary$80,000"]

tmp2$est[tmp2$salary == "$15,000" & tmp2$fundraising == "$100,000"] = coef(inter_reg2)["fundraising$100,000"]
tmp2$var[tmp2$salary == "$15,000" & tmp2$fundraising == "$100,000"] = inter_cov2["fundraising$100,000","fundraising$100,000"]

tmp2$est[tmp2$salary == "$50,000" & tmp2$fundraising == "$100,000"] = coef(inter_reg2)["salary$50,000"] + coef(inter_reg2)["fundraising$100,000"] + coef(inter_reg2)["salary$50,000:fundraising$100,000"]
tmp2$var[tmp2$salary == "$50,000" & tmp2$fundraising == "$100,000"] = inter_cov2["salary$50,000","salary$50,000"] + inter_cov2["fundraising$100,000","fundraising$100,000"] + 2 * inter_cov2["salary$50,000","fundraising$100,000"]

tmp2$est[tmp2$salary == "$80,000" & tmp2$fundraising == "$100,000"] =  coef(inter_reg2)["salary$80,000"] + coef(inter_reg2)["fundraising$100,000"] + coef(inter_reg2)["salary$80,000:fundraising$100,000"]
tmp2$var[tmp2$salary == "$80,000" & tmp2$fundraising == "$100,000"] =  inter_cov2["salary$80,000","salary$80,000"] + inter_cov2["fundraising$100,000","fundraising$100,000"] + 2 * inter_cov2["salary$80,000","fundraising$100,000"]

tmp2$est[tmp2$salary == "$15,000" & tmp2$fundraising == "$300,000"] = coef(inter_reg2)["fundraising$300,000"]
tmp2$var[tmp2$salary == "$15,000" & tmp2$fundraising == "$300,000"] = inter_cov2["fundraising$300,000","fundraising$300,000"]

tmp2$est[tmp2$salary == "$50,000" & tmp2$fundraising == "$300,000"] = coef(inter_reg2)["salary$50,000"] + coef(inter_reg2)["fundraising$300,000"] + coef(inter_reg2)["salary$50,000:fundraising$300,000"]
tmp2$var[tmp2$salary == "$50,000" & tmp2$fundraising == "$300,000"] = inter_cov2["salary$50,000","salary$50,000"] + inter_cov2["fundraising$300,000","fundraising$300,000"] + 2 * inter_cov2["salary$50,000","fundraising$100,000"]

tmp2$est[tmp2$salary == "$80,000" & tmp2$fundraising == "$300,000"] =  coef(inter_reg2)["salary$80,000"] + coef(inter_reg2)["fundraising$300,000"] + coef(inter_reg2)["salary$80,000:fundraising$300,000"]
tmp2$var[tmp2$salary == "$80,000" & tmp2$fundraising == "$300,000"] =  inter_cov2["salary$80,000","salary$80,000"] + inter_cov2["fundraising$300,000","fundraising$300,000"] + 2 * inter_cov2["salary$80,000","fundraising$100,000"]

tmp2$se = sqrt(tmp2$var)

ggplot(tmp2) + 
  aes(x = est, xmin = est - 1.96*se, xmax = est + 1.96*se, y = fundraising, colour = salary) + 
  geom_vline(xintercept = 0, lty = 3) + 
  geom_errorbarh(position = position_dodgev(height=.25), height = 0) + 
  geom_point(position = position_dodgev(height=.25)) + 
  labs(x = "AMCE on Win Probability Relative to a\nLow-Salary, Low-Fundraising Race", y = "Fundraising") + 
  scale_colour_grey(name = NULL) + 
  guides(colour = guide_legend(rev=TRUE))
ggsave("figs/prob_conjoint_salfund_interact.pdf",width=6,height=4)



## Plot marginal means
margmeans = expand.grid(salary = unique(conjoint$salary), 
                        fundraising = unique(conjoint$fundraising)) 
margmeans = na.omit(margmeans)
margmeans$mean_prob <- margmeans$se_prob <- margmeans$mean_int <- margmeans$se_int <-  NA
for (i in 1:nrow(margmeans)){
  sal = margmeans$salary[i]
  fun = margmeans$fundraising[i]
  mod_prob = lm_robust(winprob~1, clusters = responseid, subset(conjoint, salary == sal & fundraising == fun))
  mod_int = lm_robust(interest_num~1, clusters = responseid, subset(conjoint, salary == sal & fundraising == fun))
  margmeans$mean_prob[i] = mod_prob$coefficients
  margmeans$se_prob[i] = mod_prob$std.error
  margmeans$mean_int[i] = mod_int$coefficients
  margmeans$se_int[i] = mod_int$std.error
}

ggplot(margmeans) + 
  aes(x = mean_int, xmax = mean_int+1.96*se_int, xmin = mean_int - 1.96*se_int, 
      y = fundraising, colour = salary) + 
  geom_errorbarh(position = position_dodgev(height=.25), height = 0) + 
  geom_point(position = position_dodgev(height=.25)) + 
  labs(x = "Average Interest (0-1)", y = "Fundraising") +
  scale_colour_grey(name = "Salary") + 
  guides(colour = guide_legend(rev=TRUE))
ggsave("figs/interest_margmeans_sal_fund.pdf", width=5, height=4)

ggplot(margmeans) + 
  aes(x = mean_prob, xmax = mean_prob + 1.96 * se_prob, xmin = mean_prob - 1.96 * se_prob, 
      y = fundraising, colour = salary) + 
  geom_errorbarh(position = position_dodgev(height=.25), height = 0) + 
  geom_point(position = position_dodgev(height=.25)) + 
  labs(x = "Average Self-Assessed Win Probability (%)", y = "Fundraising") +
  scale_colour_grey(name = "Salary") + 
  scale_x_continuous(breaks = seq(0, 100, 5)) + 
  coord_cartesian(xlim = c(35, 65)) + 
  guides(colour = guide_legend(rev=TRUE))
ggsave("figs/prob_margmeans_sal_fund.pdf", width=5,height=4)




# Comparison of fundraising and salary ------------------------------------

# Run a regression where we allow (log) fundraising and salary to enter as 
# continuous predictors.
conjoint = conjoint %>% 
  mutate(salary_num = as.numeric(gsub("\\$|,", "", salary)) / 1000,
         fundraising_num = as.numeric(gsub("\\$|,", "", fundraising)) / 1000)

linmod = lm_robust(interest_num ~ log(salary_num) + log(fundraising_num), 
                   data = conjoint, clusters = responseid)
coef(linmod)["log(salary_num)"]
coef(linmod)["log(fundraising_num)"]

# plot the predicted responses, holding other 
sal_levs = sort(unique(conjoint$salary_num))
fun_levs = sort(unique(conjoint$fundraising_num))
sal_grid = expand.grid(salary_num = seq(15, 120, 2), fundraising_num = fun_levs)
fun_grid = expand.grid(fundraising_num = seq(25, 500, 25), salary_num = sal_levs)

sal_grid$pred = predict(linmod, newdata = sal_grid)
fun_grid$pred = predict(linmod, newdata = fun_grid)


ggplot(sal_grid) + 
  aes(x = salary_num, y = pred, lty = factor(fundraising_num)) + 
  geom_line() + 
  labs(x = "Salary", y = "Predicted response") + 
  scale_linetype(name = "Fundraising required", labels = function(x) paste0("$", x, "k")) + 
  theme(legend.position = "bottom") +
  coord_cartesian(ylim = c(.25, .5), xlim = c(10, 130)) +
  scale_y_continuous(breaks = seq(0, 1, .05)) + 
  scale_x_continuous(labels = function(x) paste0("$", x, "k"),
                     breaks = seq(25, 125, 25)) + 
  theme(text = element_text(size = 16))
ggsave("figs/preds_salary_linear_mod.pdf", width=6, height=4)

ggplot(subset(fun_grid, !is.na(salary_num))) + 
  aes(x = fundraising_num, y = pred, lty = factor(salary_num)) + 
  geom_line() + 
  labs(x = "Fundraising required", y = "Predicted response") + 
  scale_linetype(name = "Salary", labels = function(x) paste0("$", x, "k")) + 
  theme(legend.position = "bottom") + 
  coord_cartesian(ylim = c(.25, .5)) +
  scale_y_continuous(breaks = seq(0, 1, .05)) + 
  scale_x_continuous(labels = function(x) paste0("$", x, "k"),
                     breaks = seq(50, 500, 100)) + 
  theme(text = element_text(size = 16))
ggsave("figs/preds_fundraising_linear_mod.pdf", width=6, height=4)


linmod = lm(interest_num ~ log(salary_num) + log(fundraising_num) + factor(responseid), data = conjoint)
linmod_se = sqrt(diag(vcovCluster(linmod, conjoint$responseid)))
linmod2 = lm(winprob ~ log(salary_num) + log(fundraising_num) + factor(responseid), data = conjoint)
linmod2_se = sqrt(diag(vcovCluster(linmod2, conjoint$responseid)))

stargazer(list(linmod, linmod2), se = list(linmod_se, linmod2_se),
          keep = "salary|advertising|fundraising|opp",
          align = TRUE, float = FALSE, style = "ajps",
          omit.stat = c("adj.rsq","F", "SER"),
          type = "latex")





# Calculate predicted values for each possible profile, then use that to 
# give a sense of the substantive effect sizes. 
# 
# # create all profiles
# unique_noNA = function(x) unique(x[!is.na(x)])
# all_profiles = expand.grid(
#   salary = unique_noNA(conjoint$salary),
#   fundraising = unique_noNA(conjoint$fundraising),
#   advertising = unique_noNA(conjoint$advertising),
#   opp_exp = unique_noNA(conjoint$opp_exp),
#   opp_ideol = unique_noNA(conjoint$opp_ideol)
# )
# 
# # set responseID and conjoint id to level w/ median FE
# response_fes = coef(interest_reg)[grepl("responseid", names(coef(interest_reg)))]
# median_responseid = names(which.min(abs(response_fes - median(response_fes))))
# median_responseid = gsub("factor(responseid)", "", median_responseid, fixed = TRUE)
# all_profiles$responseid = median_responseid
# 
# predicted_dv = predict(interest_reg, all_profiles, se.fit = TRUE)
# all_profiles$pred_interest    = predicted_dv$fit
# all_profiles$pred_interest_se = predicted_dv$se.fit
# 
# 
# ggplot(all_profiles) +
#   aes(y = pred_interest, x = rank, colour = fundraising) +
#   geom_point(size = 1, alpha= .8) +
#   theme_bw() +
#   scale_colour_hc(name = "Required\nfundraising") +
#   # scale_x_continuous(breaks = seq(0, 100, 10)) +
#   scale_y_continuous(breaks = seq(0, 1, .1)) +
#   theme(axis.text.x =  element_blank(),
#         axis.ticks.x = element_blank()) + 
#   labs(x = "Least interest in running                                        Most interest in running", 
#        y = "Predicted probability") + 
#   coord_cartesian(ylim = c(.5, 1)) + 
#   theme(legend.position = "bottom", 
#         axis.title.x = element_text(size=12),
#         legend.text = element_text(size=12)) + 
#   facet_wrap(~salary) + 
#   guides(colour = guide_legend(ncol = 3, rev=TRUE))
# ggsave("figs/all_profiles_pred_interest.pdf", width=6,height=4)







# SUBGROUP ANALYSIS: GENDER -----------------------------------------------
conjoint = conjoint %>% 
  mutate(income_past_num = case_when(income_past == "less than $15,000" ~ 10,
                                     income_past == "$15,000-$30,000" ~ 22.5,
                                     income_past == "$30,000-$50,000" ~ 40,
                                     income_past == "$50,000-$75,000" ~ 62.5,
                                     income_past == "$75,000-$100,000" ~ 87.5,
                                     income_past == "more than $100,000" ~ 125,
                                     TRUE ~ NaN)) %>% 
  mutate(above_med_inc = ifelse(income_past_num > median(income_past_num, na.rm=TRUE), 1, 0))


refcatsabove_med_inc = refcats
refcatsabove_med_inc$inc = "Above median income"
refcatsbelow_med_inc = refcats
refcatsbelow_med_inc$inc = "Below median income"
refcats_priorinc = rbind(refcatsabove_med_inc, refcatsbelow_med_inc)

# create subsets
highinc = subset(conjoint, above_med_inc == 1)
lowinc = subset(conjoint, above_med_inc == 0)

# interactive formula -- get rid of FE b/c they're collinear w/ sex
interest_formula_priorinc = formula(interest_num ~ above_med_inc : (salary + fundraising + advertising + opp_exp + opp_ideol) +
                                      (salary + fundraising + advertising + opp_exp + opp_ideol) + factor(responseid))
prob_formula_priorinc = formula(winprob ~ above_med_inc : (salary + fundraising + advertising + opp_exp + opp_ideol) +
                                  (salary + fundraising + advertising + opp_exp + opp_ideol) + factor(responseid))



# outcome: interest, by prior income --------------------------------------------


# create conjoint plots by running separate regressions

interest_regabove_med_inc = lm(interest_formula, highinc)
interest_covabove_med_inc = vcovCluster(interest_regabove_med_inc, highinc$responseid)
interest_seabove_med_inc = sqrt(diag(interest_covabove_med_inc))



interest_regbelow_med_inc = lm(interest_formula, lowinc)
interest_covbelow_med_inc = vcovCluster(interest_regbelow_med_inc, lowinc$responseid)
interest_sebelow_med_inc = sqrt(diag(interest_covbelow_med_inc))


out_df_interestabove_med_inc = data_frame(variable = names(coef(interest_regabove_med_inc)[2:10]),
                                          coef = coef(interest_regabove_med_inc)[2:10],
                                          se = interest_seabove_med_inc[2:10],
                                          inc = "Above median income")

out_df_interestbelow_med_inc = data_frame(variable = names(coef(interest_regbelow_med_inc)[2:10]),
                                          coef = coef(interest_regbelow_med_inc)[2:10],
                                          se = interest_sebelow_med_inc[2:10],
                                          inc = "Below median income")

out_df_interest_priorinc = rbind(out_df_interestabove_med_inc, out_df_interestbelow_med_inc)

out_df_interest_priorinc = rbind(out_df_interest_priorinc, refcats_priorinc)
out_df_interest_priorinc$attribute = NA_character_
out_df_interest_priorinc$attribute[grepl("salary", out_df_interest_priorinc$variable)] = "Salary"
out_df_interest_priorinc$attribute[grepl("fundraising", out_df_interest_priorinc$variable)] = "Fundraising"
out_df_interest_priorinc$attribute[grepl("advertising", out_df_interest_priorinc$variable)] = "Advertising"
out_df_interest_priorinc$attribute[grepl("opp_exp", out_df_interest_priorinc$variable)] = "Opponent's Experience"
out_df_interest_priorinc$attribute[grepl("opp_ideol", out_df_interest_priorinc$variable)] = "Opponent's Ideology"

out_df_interest_priorinc$variable = gsub("salary|fundraising|advertising|opp_exp|opp_ideol", "", out_df_interest_priorinc$variable)

out_df_interest_priorinc$variable = factor(out_df_interest_priorinc$variable, 
                                           c("$15,000", "$50,000", "$80,000",  # salary
                                             "$25,000", "$100,000", "$300,000", # fundraising
                                             "No negative ads", "Mostly negative ads", # advertising
                                             "No prior experience", "Mayor", "Incumbent", # opponent experience
                                             "Moderate", "Somewhat ideological", "Very ideological")) # opponent ideol
out_df_interest_priorinc$attribute = factor(out_df_interest_priorinc$attribute, 
                                            c("Salary", "Fundraising", "Advertising", 
                                              "Opponent's Experience", "Opponent's Ideology"))

out_df_interest_priorinc$inc = gsub("income", "\nincome", out_df_interest_priorinc$inc)
ggplot(out_df_interest_priorinc) +
  aes(y = fct_rev(variable), x = coef, xmin = coef - 1.96*se, xmax = coef + 1.96*se, 
      colour = inc) + 
  geom_vline(xintercept = 0, lty = 3) + 
  geom_errorbarh(height = 0, position = position_dodgev(height = .5)) + 
  geom_point(position = position_dodgev(height = .5)) + 
  facet_wrap(~attribute, ncol = 1, scales = "free_y") + 
  scale_colour_hc(name = NULL) +
  guides(colour = guide_legend(rev = TRUE)) + 
  labs(x = "Average Marginal Component Effect", y = NULL) 
  # ggtitle("Effect on Interest in Running,\nby Prior Income")
ggsave("figs/interest_conjoint_priorinc.pdf", width=6, height=8)



# check more formally by running full model w/ interactions
interest_interact_priorinc = lm(interest_formula_priorinc, conjoint)
interest_interact_cov = vcovCluster(interest_interact_priorinc, conjoint$responseid)

# test whether there are significant interactions:
int = coeftest(interest_interact_priorinc, vcov. = interest_interact_cov)
cat("\n\nPRINTING OUT TABLE FOR INTERACTIONS BETWEEN TREATMENT AND PRIOR INCOME, FOR INTEREST OUTCOME:\n\n")
int[!grepl("response", rownames(int)),] 



# outcome: win probability, by prior income -------------------------------------


prob_regabove_med_inc = lm(prob_formula, highinc)
prob_covabove_med_inc = vcovCluster(prob_regabove_med_inc, highinc$responseid)
prob_seabove_med_inc = sqrt(diag(prob_covabove_med_inc))



prob_regbelow_med_inc = lm(prob_formula, lowinc)
prob_covbelow_med_inc = vcovCluster(prob_regbelow_med_inc, lowinc$responseid)
prob_sebelow_med_inc = sqrt(diag(prob_covbelow_med_inc))


out_df_probabove_med_inc = data_frame(variable = names(coef(prob_regabove_med_inc)[2:10]),
                                      coef = coef(prob_regabove_med_inc)[2:10],
                                      se = prob_seabove_med_inc[2:10],
                                      inc = "Above median income")

out_df_probbelow_med_inc = data_frame(variable = names(coef(prob_regbelow_med_inc)[2:10]),
                                      coef = coef(prob_regbelow_med_inc)[2:10],
                                      se = prob_sebelow_med_inc[2:10],
                                      inc = "Below median income")

out_df_prob_priorinc = rbind(out_df_probabove_med_inc, out_df_probbelow_med_inc)

out_df_prob_priorinc = rbind(out_df_prob_priorinc, refcats_priorinc)
out_df_prob_priorinc$attribute = NA_character_
out_df_prob_priorinc$attribute[grepl("salary", out_df_prob_priorinc$variable)] = "Salary"
out_df_prob_priorinc$attribute[grepl("fundraising", out_df_prob_priorinc$variable)] = "Fundraising"
out_df_prob_priorinc$attribute[grepl("advertising", out_df_prob_priorinc$variable)] = "Advertising"
out_df_prob_priorinc$attribute[grepl("opp_exp", out_df_prob_priorinc$variable)] = "Opponent's Experience"
out_df_prob_priorinc$attribute[grepl("opp_ideol", out_df_prob_priorinc$variable)] = "Opponent's Ideology"

out_df_prob_priorinc$variable = gsub("salary|fundraising|advertising|opp_exp|opp_ideol", "", out_df_prob_priorinc$variable)

out_df_prob_priorinc$variable = factor(out_df_prob_priorinc$variable, 
                                       c("$15,000", "$50,000", "$80,000",  # salary
                                         "$25,000", "$100,000", "$300,000", # fundraising
                                         "No negative ads", "Mostly negative ads", # advertising
                                         "No prior experience", "Mayor", "Incumbent", # opponent experience
                                         "Moderate", "Somewhat ideological", "Very ideological")) # opponent ideol
out_df_prob_priorinc$attribute = factor(out_df_prob_priorinc$attribute, 
                                        c("Salary", "Fundraising", "Advertising", 
                                          "Opponent's Experience", "Opponent's Ideology"))

out_df_prob_priorinc$inc = gsub("income", "\nincome", out_df_prob_priorinc$inc)
ggplot(out_df_prob_priorinc) +
  aes(y = fct_rev(variable), x = coef, xmin = coef - 1.96*se, xmax = coef + 1.96*se, 
      colour = inc) + 
  geom_vline(xintercept = 0, lty = 3) + 
  geom_errorbarh(height = 0, position = position_dodgev(height = .5)) + 
  geom_point(position = position_dodgev(height = .5)) + 
  facet_wrap(~attribute, ncol = 1, scales = "free_y") + 
  scale_colour_hc(name = NULL) +
  guides(colour = guide_legend(rev = TRUE)) + 
  labs(x = "Average Marginal Component Effect", y = NULL) 
  # ggtitle("Effect on Self-Assessed Win Probability,\nby Prior Income")
ggsave("figs/prob_conjoint_priorinc.pdf", width=6, height=8)


# check more formally by running full model w/ interactions
prob_interact_priorinc = lm(prob_formula_priorinc, conjoint)
prob_interact_cov = vcovCluster(prob_interact_priorinc, conjoint$responseid)

# test whether there are significant interactions:
int = coeftest(prob_interact_priorinc, vcov. = prob_interact_cov)
cat("\n\nPRINTING OUT TABLE FOR INTERACTIONS BETWEEN TREATMENT AND PRIOR INCOME, FOR PROBABILITY OUTCOME:\n\n")
int[!grepl("response", rownames(int)),] 









# SUBGROUP ANALYSIS: GENDER -----------------------------------------------


conjoint$male = ifelse(conjoint$Gender == "Male", 1,
                       ifelse(conjoint$Gender == "Female", 0, NA))


refcats_men = refcats
refcats_men$sex = "Men"
refcats_women = refcats
refcats_women$sex = "Women"
refcats_gender = rbind(refcats_men, refcats_women)

# create subsets
men = subset(conjoint, male == 1)
women = subset(conjoint, male == 0)

# interactive formula -- get rid of FE b/c they're collinear w/ sex
interest_formula_gender = formula(interest_num ~ male : (salary + fundraising + advertising + opp_exp + opp_ideol) +
                                    (salary + fundraising + advertising + opp_exp + opp_ideol) + factor(responseid))
prob_formula_gender = formula(winprob ~ male : (salary + fundraising + advertising + opp_exp + opp_ideol) +
                                (salary + fundraising + advertising + opp_exp + opp_ideol) + factor(responseid))



# outcome: interest, by gender --------------------------------------------


# create conjoint plots by running separate regressions

interest_reg_men = lm(interest_formula, men)
interest_cov_men = vcovCluster(interest_reg_men, men$responseid)
interest_se_men = sqrt(diag(interest_cov_men))



interest_reg_women = lm(interest_formula, women)
interest_cov_women = vcovCluster(interest_reg_women, women$responseid)
interest_se_women = sqrt(diag(interest_cov_women))


out_df_interest_men = data_frame(variable = names(coef(interest_reg_men)[2:10]),
                                 coef = coef(interest_reg_men)[2:10],
                                 se = interest_se_men[2:10],
                                 sex = "Men")

out_df_interest_women = data_frame(variable = names(coef(interest_reg_women)[2:10]),
                                   coef = coef(interest_reg_women)[2:10],
                                   se = interest_se_women[2:10],
                                   sex = "Women")

out_df_interest_gender = rbind(out_df_interest_men, out_df_interest_women)

out_df_interest_gender = rbind(out_df_interest_gender, refcats_gender)
out_df_interest_gender$attribute = NA_character_
out_df_interest_gender$attribute[grepl("salary", out_df_interest_gender$variable)] = "Salary"
out_df_interest_gender$attribute[grepl("fundraising", out_df_interest_gender$variable)] = "Fundraising"
out_df_interest_gender$attribute[grepl("advertising", out_df_interest_gender$variable)] = "Advertising"
out_df_interest_gender$attribute[grepl("opp_exp", out_df_interest_gender$variable)] = "Opponent's Experience"
out_df_interest_gender$attribute[grepl("opp_ideol", out_df_interest_gender$variable)] = "Opponent's Ideology"

out_df_interest_gender$variable = gsub("salary|fundraising|advertising|opp_exp|opp_ideol", "", out_df_interest_gender$variable)

out_df_interest_gender$variable = factor(out_df_interest_gender$variable, 
                                         c("$15,000", "$50,000", "$80,000",  # salary
                                           "$25,000", "$100,000", "$300,000", # fundraising
                                           "No negative ads", "Mostly negative ads", # advertising
                                           "No prior experience", "Mayor", "Incumbent", # opponent experience
                                           "Moderate", "Somewhat ideological", "Very ideological")) # opponent ideol
out_df_interest_gender$attribute = factor(out_df_interest_gender$attribute, 
                                          c("Salary", "Fundraising", "Advertising", 
                                            "Opponent's Experience", "Opponent's Ideology"))

ggplot(out_df_interest_gender) +
  aes(y = fct_rev(variable), x = coef, xmin = coef - 1.96*se, xmax = coef + 1.96*se, 
      colour = sex) + 
  geom_vline(xintercept = 0, lty = 3) + 
  geom_errorbarh(height = 0, position = position_dodgev(height = .5)) + 
  geom_point(position = position_dodgev(height = .5)) + 
  facet_wrap(~attribute, ncol = 1, scales = "free_y") + 
  scale_colour_hc(name = NULL) +
  guides(colour = guide_legend(rev = TRUE)) + 
  labs(x = "Average Marginal Component Effect", y = NULL)
  # ggtitle("Effect on Interest in Running, by Gender")
ggsave("figs/interest_conjoint_gender.pdf", width=6, height=8)



# check more formally by running full model w/ interactions
interest_interact_gender = lm(interest_formula_gender, conjoint)
interest_interact_cov = vcovCluster(interest_interact_gender, conjoint$responseid)

# test whether there are significant interactions:
int = coeftest(interest_interact_gender, vcov. = interest_interact_cov)
cat("\n\nPRINTING OUT TABLE FOR INTERACTIONS BETWEEN TREATMENT AND SEX, FOR INTEREST OUTCOME:\n\n")
int[!grepl("response", rownames(int)),] 



# outcome: win probability, by gender -------------------------------------


prob_reg_men = lm(prob_formula, men)
prob_cov_men = vcovCluster(prob_reg_men, men$responseid)
prob_se_men = sqrt(diag(prob_cov_men))



prob_reg_women = lm(prob_formula, women)
prob_cov_women = vcovCluster(prob_reg_women, women$responseid)
prob_se_women = sqrt(diag(prob_cov_women))


out_df_prob_men = data_frame(variable = names(coef(prob_reg_men)[2:10]),
                             coef = coef(prob_reg_men)[2:10],
                             se = prob_se_men[2:10],
                             sex = "Men")

out_df_prob_women = data_frame(variable = names(coef(prob_reg_women)[2:10]),
                               coef = coef(prob_reg_women)[2:10],
                               se = prob_se_women[2:10],
                               sex = "Women")

out_df_prob_gender = rbind(out_df_prob_men, out_df_prob_women)

out_df_prob_gender = rbind(out_df_prob_gender, refcats_gender)
out_df_prob_gender$attribute = NA_character_
out_df_prob_gender$attribute[grepl("salary", out_df_prob_gender$variable)] = "Salary"
out_df_prob_gender$attribute[grepl("fundraising", out_df_prob_gender$variable)] = "Fundraising"
out_df_prob_gender$attribute[grepl("advertising", out_df_prob_gender$variable)] = "Advertising"
out_df_prob_gender$attribute[grepl("opp_exp", out_df_prob_gender$variable)] = "Opponent's Experience"
out_df_prob_gender$attribute[grepl("opp_ideol", out_df_prob_gender$variable)] = "Opponent's Ideology"

out_df_prob_gender$variable = gsub("salary|fundraising|advertising|opp_exp|opp_ideol", "", out_df_prob_gender$variable)

out_df_prob_gender$variable = factor(out_df_prob_gender$variable, 
                                     c("$15,000", "$50,000", "$80,000",  # salary
                                       "$25,000", "$100,000", "$300,000", # fundraising
                                       "No negative ads", "Mostly negative ads", # advertising
                                       "No prior experience", "Mayor", "Incumbent", # opponent experience
                                       "Moderate", "Somewhat ideological", "Very ideological")) # opponent ideol
out_df_prob_gender$attribute = factor(out_df_prob_gender$attribute, 
                                      c("Salary", "Fundraising", "Advertising", 
                                        "Opponent's Experience", "Opponent's Ideology"))

ggplot(out_df_prob_gender) +
  aes(y = fct_rev(variable), x = coef, xmin = coef - 1.96*se, xmax = coef + 1.96*se, 
      colour = sex) + 
  geom_vline(xintercept = 0, lty = 3) + 
  geom_errorbarh(height = 0, position = position_dodgev(height = .5)) + 
  geom_point(position = position_dodgev(height = .5)) + 
  facet_wrap(~attribute, ncol = 1, scales = "free_y") + 
  scale_colour_hc(name = NULL) +
  guides(colour = guide_legend(rev = TRUE)) + 
  labs(x = "Average Marginal Component Effect", y = NULL) 
  # ggtitle("Effect on Self-Assessed Win Probability, by Gender")
ggsave("figs/prob_conjoint_gender.pdf", width=6, height=8)


# check more formally by running full model w/ interactions
prob_interact_gender = lm(prob_formula_gender, conjoint)
prob_interact_cov = vcovCluster(prob_interact_gender, conjoint$responseid)

# test whether there are significant interactions:
int = coeftest(prob_interact_gender, vcov. = prob_interact_cov)
cat("\n\nPRINTING OUT TABLE FOR INTERACTIONS BETWEEN TREATMENT AND SEX, FOR PROBABILITY OUTCOME:\n\n")
int[!grepl("response", rownames(int)),] 









# SUBGROUP ANALYSIS: ELECTED OFFICIALS ------------------------------------




conjoint = conjoint %>% 
  rename(elected = Elected)


refcats_elected = refcats
refcats_elected$elected = "Elected"
refcats_unelected = refcats
refcats_unelected$elected = "Unelected"
refcats_elected = rbind(refcats_elected, refcats_unelected)

# create subsets
elected = subset(conjoint, elected == 1)
unelected = subset(conjoint, elected == 0)

# interactive formula -- get rid of FE b/c they're collinear 
interest_formula_elect = formula(interest_num ~ elected : (salary + fundraising + advertising + opp_exp + opp_ideol) +
                                   (salary + fundraising + advertising + opp_exp + opp_ideol) + factor(responseid))
prob_formula_elect = formula(winprob ~ elected : (salary + fundraising + advertising + opp_exp + opp_ideol) +
                               (salary + fundraising + advertising + opp_exp + opp_ideol) + factor(responseid))



# outcome: interest, by elected --------------------------------------------


# create conjoint plots by running separate regressions

interest_reg_elected = lm(interest_formula, elected)
interest_cov_elected = vcovCluster(interest_reg_elected, elected$responseid)
interest_se_elected = sqrt(diag(interest_cov_elected))



interest_reg_unelected = lm(interest_formula, unelected)
interest_cov_unelected = vcovCluster(interest_reg_unelected, unelected$responseid)
interest_se_unelected = sqrt(diag(interest_cov_unelected))


out_df_interest_elected = data_frame(variable = names(coef(interest_reg_elected)[2:10]),
                                     coef = coef(interest_reg_elected)[2:10],
                                     se = interest_se_elected[2:10],
                                     elected = "Elected")

out_df_interest_unelected = data_frame(variable = names(coef(interest_reg_unelected)[2:10]),
                                       coef = coef(interest_reg_unelected)[2:10],
                                       se = interest_se_unelected[2:10],
                                       elected = "Unelected")

out_df_interest_elected = rbind(out_df_interest_elected, out_df_interest_unelected)

out_df_interest_elected = rbind(out_df_interest_elected, refcats_elected)
out_df_interest_elected$attribute = NA_character_
out_df_interest_elected$attribute[grepl("salary", out_df_interest_elected$variable)] = "Salary"
out_df_interest_elected$attribute[grepl("fundraising", out_df_interest_elected$variable)] = "Fundraising"
out_df_interest_elected$attribute[grepl("advertising", out_df_interest_elected$variable)] = "Advertising"
out_df_interest_elected$attribute[grepl("opp_exp", out_df_interest_elected$variable)] = "Opponent's Experience"
out_df_interest_elected$attribute[grepl("opp_ideol", out_df_interest_elected$variable)] = "Opponent's Ideology"

out_df_interest_elected$variable = gsub("salary|fundraising|advertising|opp_exp|opp_ideol", "", out_df_interest_elected$variable)

out_df_interest_elected$variable = factor(out_df_interest_elected$variable, 
                                          c("$15,000", "$50,000", "$80,000",  # salary
                                            "$25,000", "$100,000", "$300,000", # fundraising
                                            "No negative ads", "Mostly negative ads", # advertising
                                            "No prior experience", "Mayor", "Incumbent", # opponent experience
                                            "Moderate", "Somewhat ideological", "Very ideological")) # opponent ideol
out_df_interest_elected$attribute = factor(out_df_interest_elected$attribute, 
                                           c("Salary", "Fundraising", "Advertising", 
                                             "Opponent's Experience", "Opponent's Ideology"))

ggplot(out_df_interest_elected) +
  aes(y = fct_rev(variable), x = coef, xmin = coef - 1.96*se, xmax = coef + 1.96*se, 
      colour = elected) + 
  geom_vline(xintercept = 0, lty = 3) + 
  geom_errorbarh(height = 0, position = position_dodgev(height = .5)) + 
  geom_point(position = position_dodgev(height = .5)) + 
  facet_wrap(~attribute, ncol = 1, scales = "free_y") + 
  scale_colour_hc(name = NULL) +
  guides(colour = guide_legend(rev = TRUE)) + 
  labs(x = "Average Marginal Component Effect", y = NULL) 
  # ggtitle("Effect on Interest in Running, by Elected/Unelected Status")
ggsave("figs/interest_conjoint_elected.pdf", width=6, height=8)



# check more formally by running full model w/ interactions
interest_interact_elect = lm(interest_formula_elect, conjoint)
interest_interact_cov = vcovCluster(interest_interact_elect, conjoint$responseid)

# test whether there are significant interactions:
int = coeftest(interest_interact_elect, vcov. = interest_interact_cov)
cat("\n\nPRINTING OUT TABLE FOR INTERACTIONS BETWEEN TREATMENT AND ELECTED STATUS, FOR INTEREST OUTCOME:\n\n")
int[!grepl("response", rownames(int)),] 





# outcome: win probability, by elected ----------------------------------------


# create conjoint plots by running separate regressions

prob_reg_elected = lm(prob_formula, elected)
prob_cov_elected = vcovCluster(prob_reg_elected, elected$responseid)
prob_se_elected = sqrt(diag(prob_cov_elected))



prob_reg_unelected = lm(prob_formula, unelected)
prob_cov_unelected = vcovCluster(prob_reg_unelected, unelected$responseid)
prob_se_unelected = sqrt(diag(prob_cov_unelected))


out_df_prob_elected = data_frame(variable = names(coef(prob_reg_elected)[2:10]),
                                 coef = coef(prob_reg_elected)[2:10],
                                 se = prob_se_elected[2:10],
                                 elected = "Elected")

out_df_prob_unelected = data_frame(variable = names(coef(prob_reg_unelected)[2:10]),
                                   coef = coef(prob_reg_unelected)[2:10],
                                   se = prob_se_unelected[2:10],
                                   elected = "Unelected")

out_df_prob_elected = rbind(out_df_prob_elected, out_df_prob_unelected)

out_df_prob_elected = rbind(out_df_prob_elected, refcats_elected)
out_df_prob_elected$attribute = NA_character_
out_df_prob_elected$attribute[grepl("salary", out_df_prob_elected$variable)] = "Salary"
out_df_prob_elected$attribute[grepl("fundraising", out_df_prob_elected$variable)] = "Fundraising"
out_df_prob_elected$attribute[grepl("advertising", out_df_prob_elected$variable)] = "Advertising"
out_df_prob_elected$attribute[grepl("opp_exp", out_df_prob_elected$variable)] = "Opponent's Experience"
out_df_prob_elected$attribute[grepl("opp_ideol", out_df_prob_elected$variable)] = "Opponent's Ideology"

out_df_prob_elected$variable = gsub("salary|fundraising|advertising|opp_exp|opp_ideol", "", out_df_prob_elected$variable)

out_df_prob_elected$variable = factor(out_df_prob_elected$variable, 
                                      c("$15,000", "$50,000", "$80,000",  # salary
                                        "$25,000", "$100,000", "$300,000", # fundraising
                                        "No negative ads", "Mostly negative ads", # advertising
                                        "No prior experience", "Mayor", "Incumbent", # opponent experience
                                        "Moderate", "Somewhat ideological", "Very ideological")) # opponent ideol
out_df_prob_elected$attribute = factor(out_df_prob_elected$attribute, 
                                       c("Salary", "Fundraising", "Advertising", 
                                         "Opponent's Experience", "Opponent's Ideology"))

ggplot(out_df_prob_elected) +
  aes(y = fct_rev(variable), x = coef, xmin = coef - 1.96*se, xmax = coef + 1.96*se, 
      colour = elected) + 
  geom_vline(xintercept = 0, lty = 3) + 
  geom_errorbarh(height = 0, position = position_dodgev(height = .5)) + 
  geom_point(position = position_dodgev(height = .5)) + 
  facet_wrap(~attribute, ncol = 1, scales = "free_y") + 
  scale_colour_hc(name = NULL) +
  guides(colour = guide_legend(rev = TRUE)) + 
  labs(x = "Average Marginal Component Effect", y = NULL) 
  # ggtitle("Effect on Self-Assessed Win Probability, by Elected/Unelected Status")
ggsave("figs/prob_conjoint_elected.pdf", width=6, height=8)



# check more formally by running full model w/ interactions
prob_interact_elect = lm(prob_formula_elect, conjoint)
prob_interact_cov = vcovCluster(prob_interact_elect, conjoint$responseid)

# test whether there are significant interactions:
int = coeftest(prob_interact_elect, vcov. = prob_interact_cov)
cat("\n\nPRINTING OUT TABLE FOR INTERACTIONS BETWEEN TREATMENT AND ELECTED STATUS, FOR prob OUTCOME:\n\n")
int[!grepl("response", rownames(int)),] 




# SUBGROUP ANALYSIS: PARTY ID ---------------------------------------------


conjoint$party = with(conjoint, ifelse(PID_3 %in% c("Independent", "Other party (Please specify):"), "Independent/Other", PID_3))

refcats_dem = refcats
refcats_dem$party = "Democrat"
refcats_rep = refcats
refcats_rep$party = "Republican"
refcats_indp = refcats
refcats_indp$party = "Independent/Other"
refcats_party = rbind(refcats_dem, refcats_rep, refcats_indp)


# create subsets
dems = subset(conjoint, party == "Democrat")
reps = subset(conjoint, party == "Republican")
indps = subset(conjoint, party == "Independent/Other")



# interactive formula -- get rid of FE b/c they're collinear 
interest_formula_party = formula(interest_num ~ party : (salary + fundraising + advertising + opp_exp + opp_ideol) +
                                   (salary + fundraising + advertising + opp_exp + opp_ideol) + factor(responseid))
prob_formula_party = formula(winprob ~ party : (salary + fundraising + advertising + opp_exp + opp_ideol) +
                               (salary + fundraising + advertising + opp_exp + opp_ideol) + factor(responseid))



# outcome: interest, by party --------------------------------------------


# create conjoint plots by running separate regressions

interest_reg_dem = lm(interest_formula, dems)
interest_cov_dem = vcovCluster(interest_reg_dem, dems$responseid)
interest_se_dem = sqrt(diag(interest_cov_dem))


interest_reg_rep = lm(interest_formula, reps)
interest_cov_rep = vcovCluster(interest_reg_rep, reps$responseid)
interest_se_rep = sqrt(diag(interest_cov_rep))

interest_reg_indp = lm(interest_formula, indps)
interest_cov_indp = vcovCluster(interest_reg_indp, indps$responseid)
interest_se_indp = sqrt(diag(interest_cov_indp))



out_df_interest_dem = data_frame(variable = names(coef(interest_reg_dem)[2:10]),
                                 coef = coef(interest_reg_dem)[2:10],
                                 se = interest_se_dem[2:10],
                                 party = "Democrat")

out_df_interest_rep = data_frame(variable = names(coef(interest_reg_rep)[2:10]),
                                 coef = coef(interest_reg_rep)[2:10],
                                 se = interest_se_rep[2:10],
                                 party = "Republican")

out_df_interest_indp = data_frame(variable = names(coef(interest_reg_indp)[2:10]),
                                  coef = coef(interest_reg_indp)[2:10],
                                  se = interest_se_indp[2:10],
                                  party = "Independent")

out_df_interest_party = rbind(out_df_interest_dem, out_df_interest_rep, out_df_interest_indp)

out_df_interest_party = rbind(out_df_interest_party, refcats_party)
out_df_interest_party$attribute = NA_character_
out_df_interest_party$attribute[grepl("salary", out_df_interest_party$variable)] = "Salary"
out_df_interest_party$attribute[grepl("fundraising", out_df_interest_party$variable)] = "Fundraising"
out_df_interest_party$attribute[grepl("advertising", out_df_interest_party$variable)] = "Advertising"
out_df_interest_party$attribute[grepl("opp_exp", out_df_interest_party$variable)] = "Opponent's Experience"
out_df_interest_party$attribute[grepl("opp_ideol", out_df_interest_party$variable)] = "Opponent's Ideology"

out_df_interest_party$variable = gsub("salary|fundraising|advertising|opp_exp|opp_ideol", "", out_df_interest_party$variable)

out_df_interest_party$variable = factor(out_df_interest_party$variable, 
                                        c("$15,000", "$50,000", "$80,000",  # salary
                                          "$25,000", "$100,000", "$300,000", # fundraising
                                          "No negative ads", "Mostly negative ads", # advertising
                                          "No prior experience", "Mayor", "Incumbent", # opponent experience
                                          "Moderate", "Somewhat ideological", "Very ideological")) # opponent ideol
out_df_interest_party$attribute = factor(out_df_interest_party$attribute, 
                                         c("Salary", "Fundraising", "Advertising", 
                                           "Opponent's Experience", "Opponent's Ideology"))


out_df_interest_party$party = factor(out_df_interest_party$party, c("Democrat", "Independent", "Republican"))


ggplot(subset(out_df_interest_party, party != "Independent")) +
  aes(y = fct_rev(variable), x = coef, xmin = coef - 1.96*se, xmax = coef + 1.96*se, 
      colour = party) + 
  geom_vline(xintercept = 0, lty = 3) + 
  geom_errorbarh(height = 0, position = position_dodgev(height = .5)) + 
  geom_point(position = position_dodgev(height = .5)) + 
  facet_wrap(~attribute, ncol = 1, scales = "free_y") + 
  scale_colour_manual(name = NULL, values = c("Republican" = "red", "Democrat" = "blue")) +
  guides(colour = guide_legend(rev = TRUE)) + 
  labs(x = "Average Marginal Component Effect", y = NULL) 
  # ggtitle("Effect on Interest in Running, by Party")
ggsave("figs/interest_conjoint_party.pdf", width=6, height=8)



# check more formally by running full model w/ interactions
interest_interact_party = lm(interest_formula_party, conjoint)
interest_interact_cov = vcovCluster(interest_interact_party, conjoint$responseid)

# test whether there are significant interactions:
int = coeftest(interest_interact_party, vcov. = interest_interact_cov)
cat("\n\nPRINTING OUT TABLE FOR INTERACTIONS BETWEEN TREATMENT AND PARTY, FOR INTEREST OUTCOME:\n\n")
int[!grepl("response", rownames(int)),] 






# outcome: win probability, by party --------------------------------------


# create conjoint plots by running separate regressions

prob_reg_dem = lm(prob_formula, dems)
prob_cov_dem = vcovCluster(prob_reg_dem, dems$responseid)
prob_se_dem = sqrt(diag(prob_cov_dem))


prob_reg_rep = lm(prob_formula, reps)
prob_cov_rep = vcovCluster(prob_reg_rep, reps$responseid)
prob_se_rep = sqrt(diag(prob_cov_rep))

prob_reg_indp = lm(prob_formula, indps)
prob_cov_indp = vcovCluster(prob_reg_indp, indps$responseid)
prob_se_indp = sqrt(diag(prob_cov_indp))



out_df_prob_dem = data_frame(variable = names(coef(prob_reg_dem)[2:10]),
                             coef = coef(prob_reg_dem)[2:10],
                             se = prob_se_dem[2:10],
                             party = "Democrat")

out_df_prob_rep = data_frame(variable = names(coef(prob_reg_rep)[2:10]),
                             coef = coef(prob_reg_rep)[2:10],
                             se = prob_se_rep[2:10],
                             party = "Republican")

out_df_prob_indp = data_frame(variable = names(coef(prob_reg_indp)[2:10]),
                              coef = coef(prob_reg_indp)[2:10],
                              se = prob_se_indp[2:10],
                              party = "Independent")

out_df_prob_party = rbind(out_df_prob_dem, out_df_prob_rep, out_df_prob_indp)

out_df_prob_party = rbind(out_df_prob_party, refcats_party)
out_df_prob_party$attribute = NA_character_
out_df_prob_party$attribute[grepl("salary", out_df_prob_party$variable)] = "Salary"
out_df_prob_party$attribute[grepl("fundraising", out_df_prob_party$variable)] = "Fundraising"
out_df_prob_party$attribute[grepl("advertising", out_df_prob_party$variable)] = "Advertising"
out_df_prob_party$attribute[grepl("opp_exp", out_df_prob_party$variable)] = "Opponent's Experience"
out_df_prob_party$attribute[grepl("opp_ideol", out_df_prob_party$variable)] = "Opponent's Ideology"

out_df_prob_party$variable = gsub("salary|fundraising|advertising|opp_exp|opp_ideol", "", out_df_prob_party$variable)

out_df_prob_party$variable = factor(out_df_prob_party$variable, 
                                    c("$15,000", "$50,000", "$80,000",  # salary
                                      "$25,000", "$100,000", "$300,000", # fundraising
                                      "No negative ads", "Mostly negative ads", # advertising
                                      "No prior experience", "Mayor", "Incumbent", # opponent experience
                                      "Moderate", "Somewhat ideological", "Very ideological")) # opponent ideol
out_df_prob_party$attribute = factor(out_df_prob_party$attribute, 
                                     c("Salary", "Fundraising", "Advertising", 
                                       "Opponent's Experience", "Opponent's Ideology"))


out_df_prob_party$party = factor(out_df_prob_party$party, c("Democrat", "Independent", "Republican"))


ggplot(subset(out_df_prob_party, party != "Independent")) +
  aes(y = fct_rev(variable), x = coef, xmin = coef - 1.96*se, xmax = coef + 1.96*se, 
      colour = party) + 
  geom_vline(xintercept = 0, lty = 3) + 
  geom_errorbarh(height = 0, position = position_dodgev(height = .5)) + 
  geom_point(position = position_dodgev(height = .5)) + 
  facet_wrap(~attribute, ncol = 1, scales = "free_y") + 
  scale_colour_manual(name = NULL, values = c("Republican" = "red", "Democrat" = "blue")) +
  guides(colour = guide_legend(rev = TRUE)) + 
  labs(x = "Average Marginal Component Effect", y = NULL) 
  # ggtitle("Effect on Self-Assessed Win Probability, by Party")
ggsave("figs/prob_conjoint_party.pdf", width=6, height=8)



# check more formally by running full model w/ interactions
prob_interact_party = lm(prob_formula_party, conjoint)
prob_interact_cov = vcovCluster(prob_interact_party, conjoint$responseid)

# test whether there are significant interactions:
int = coeftest(prob_interact_party, vcov. = prob_interact_cov)
cat("\n\nPRINTING OUT TABLE FOR INTERACTIONS BETWEEN TREATMENT AND PARTY, FOR prob OUTCOME:\n\n")
int[!grepl("response", rownames(int)),] 








#### SUBGROUP ANALYSIS: OCCUPATION STATUS #####

blue_collar = c("Farmer", "Skilled Trade (e.g., construction, manufacturing, plumbing, etc.)",
                "Union Officer", "Transportation/Driving")
jobvars = names(conjoint)[grepl("^job_", names(conjoint))]

bc = apply(conjoint[,jobvars], 1, function(x) any(x %in% blue_collar))
conjoint$blue_collar = bc



refcats_wc = refcats
refcats_wc$occstatus = "White Collar"
refcats_bc = refcats
refcats_bc$occstatus = "Blue Collar"
refcats_occstatus = rbind(refcats_wc, refcats_bc)


# create subsets
wcs = subset(conjoint, blue_collar == FALSE)
bcs = subset(conjoint, blue_collar == TRUE)




# interactive formula -- get rid of FE b/c they're collinear 
interest_formula_occstatus = formula(interest_num ~ blue_collar : (salary + fundraising + advertising + opp_exp + opp_ideol) +
                                       (salary + fundraising + advertising + opp_exp + opp_ideol) + factor(responseid))
prob_formula_occstatus = formula(winprob ~ blue_collar : (salary + fundraising + advertising + opp_exp + opp_ideol) +
                                   (salary + fundraising + advertising + opp_exp + opp_ideol) + factor(responseid))



# outcome: interest, by occstatus --------------------------------------------


# create conjoint plots by running separate regressions

interest_reg_wc = lm(interest_formula, wcs)
interest_cov_wc = vcovCluster(interest_reg_wc, wcs$responseid)
interest_se_wc = sqrt(diag(interest_cov_wc))


interest_reg_bc = lm(interest_formula, bcs)
interest_cov_bc = vcovCluster(interest_reg_bc, bcs$responseid)
interest_se_bc = sqrt(diag(interest_cov_bc))

out_df_interest_wc = data_frame(variable = names(coef(interest_reg_wc)[2:10]),
                                coef = coef(interest_reg_wc)[2:10],
                                se = interest_se_wc[2:10],
                                occstatus = "White Collar")

out_df_interest_bc = data_frame(variable = names(coef(interest_reg_bc)[2:10]),
                                coef = coef(interest_reg_bc)[2:10],
                                se = interest_se_bc[2:10],
                                occstatus = "Blue Collar")


out_df_interest_occstatus = rbind(out_df_interest_wc, out_df_interest_bc)

out_df_interest_occstatus = rbind(out_df_interest_occstatus, refcats_occstatus)
out_df_interest_occstatus$attribute = NA_character_
out_df_interest_occstatus$attribute[grepl("salary", out_df_interest_occstatus$variable)] = "Salary"
out_df_interest_occstatus$attribute[grepl("fundraising", out_df_interest_occstatus$variable)] = "Fundraising"
out_df_interest_occstatus$attribute[grepl("advertising", out_df_interest_occstatus$variable)] = "Advertising"
out_df_interest_occstatus$attribute[grepl("opp_exp", out_df_interest_occstatus$variable)] = "Opponent's Experience"
out_df_interest_occstatus$attribute[grepl("opp_ideol", out_df_interest_occstatus$variable)] = "Opponent's Ideology"

out_df_interest_occstatus$variable = gsub("salary|fundraising|advertising|opp_exp|opp_ideol", "", out_df_interest_occstatus$variable)

out_df_interest_occstatus$variable = factor(out_df_interest_occstatus$variable, 
                                            c("$15,000", "$50,000", "$80,000",  # salary
                                              "$25,000", "$100,000", "$300,000", # fundraising
                                              "No negative ads", "Mostly negative ads", # advertising
                                              "No prior experience", "Mayor", "Incumbent", # opponent experience
                                              "Moderate", "Somewhat ideological", "Very ideological")) # opponent ideol
out_df_interest_occstatus$attribute = factor(out_df_interest_occstatus$attribute, 
                                             c("Salary", "Fundraising", "Advertising", 
                                               "Opponent's Experience", "Opponent's Ideology"))


out_df_interest_occstatus$occstatus = factor(out_df_interest_occstatus$occstatus, c("White Collar", "Blue Collar"))


ggplot(out_df_interest_occstatus) +
  aes(y = fct_rev(variable), x = coef, xmin = coef - 1.96*se, xmax = coef + 1.96*se, 
      colour = occstatus) + 
  geom_vline(xintercept = 0, lty = 3) + 
  geom_errorbarh(height = 0, position = position_dodgev(height = .5)) + 
  geom_point(position = position_dodgev(height = .5)) + 
  facet_wrap(~attribute, ncol = 1, scales = "free_y") + 
  scale_colour_manual(name = NULL, values = c("White Collar" = "black", "Blue Collar" = "darkblue")) +
  guides(colour = guide_legend(rev = TRUE)) + 
  labs(x = "Average Marginal Component Effect", y = NULL) +
  ggtitle("Effect on Interest in Running, by Occupation Status")
ggsave("figs/interest_conjoint_occstatus.pdf", width=6, height=8)



# check more formally by running full model w/ interactions
interest_interact_occstatus = lm(interest_formula_occstatus, conjoint)
interest_interact_cov = vcovCluster(interest_interact_occstatus, conjoint$responseid)

# test whether there are significant interactions:
int = coeftest(interest_interact_occstatus, vcov. = interest_interact_cov)
cat("\n\nPRINTING OUT TABLE FOR INTERACTIONS BETWEEN TREATMENT AND occstatus, FOR INTEREST OUTCOME:\n\n")
int[!grepl("response", rownames(int)),] 







# outcome: prob, by occstatus --------------------------------------------


# create conjoint plots by running separate regressions

prob_reg_wc = lm(prob_formula, wcs)
prob_cov_wc = vcovCluster(prob_reg_wc, wcs$responseid)
prob_se_wc = sqrt(diag(prob_cov_wc))


prob_reg_bc = lm(prob_formula, bcs)
prob_cov_bc = vcovCluster(prob_reg_bc, bcs$responseid)
prob_se_bc = sqrt(diag(prob_cov_bc))

out_df_prob_wc = data_frame(variable = names(coef(prob_reg_wc)[2:10]),
                            coef = coef(prob_reg_wc)[2:10],
                            se = prob_se_wc[2:10],
                            occstatus = "White Collar")

out_df_prob_bc = data_frame(variable = names(coef(prob_reg_bc)[2:10]),
                            coef = coef(prob_reg_bc)[2:10],
                            se = prob_se_bc[2:10],
                            occstatus = "Blue Collar")


out_df_prob_occstatus = rbind(out_df_prob_wc, out_df_prob_bc)

out_df_prob_occstatus = rbind(out_df_prob_occstatus, refcats_occstatus)
out_df_prob_occstatus$attribute = NA_character_
out_df_prob_occstatus$attribute[grepl("salary", out_df_prob_occstatus$variable)] = "Salary"
out_df_prob_occstatus$attribute[grepl("fundraising", out_df_prob_occstatus$variable)] = "Fundraising"
out_df_prob_occstatus$attribute[grepl("advertising", out_df_prob_occstatus$variable)] = "Advertising"
out_df_prob_occstatus$attribute[grepl("opp_exp", out_df_prob_occstatus$variable)] = "Opponent's Experience"
out_df_prob_occstatus$attribute[grepl("opp_ideol", out_df_prob_occstatus$variable)] = "Opponent's Ideology"

out_df_prob_occstatus$variable = gsub("salary|fundraising|advertising|opp_exp|opp_ideol", "", out_df_prob_occstatus$variable)

out_df_prob_occstatus$variable = factor(out_df_prob_occstatus$variable, 
                                        c("$15,000", "$50,000", "$80,000",  # salary
                                          "$25,000", "$100,000", "$300,000", # fundraising
                                          "No negative ads", "Mostly negative ads", # advertising
                                          "No prior experience", "Mayor", "Incumbent", # opponent experience
                                          "Moderate", "Somewhat ideological", "Very ideological")) # opponent ideol
out_df_prob_occstatus$attribute = factor(out_df_prob_occstatus$attribute, 
                                         c("Salary", "Fundraising", "Advertising", 
                                           "Opponent's Experience", "Opponent's Ideology"))


out_df_prob_occstatus$occstatus = factor(out_df_prob_occstatus$occstatus, c("White Collar", "Blue Collar"))


ggplot(out_df_prob_occstatus) +
  aes(y = fct_rev(variable), x = coef, xmin = coef - 1.96*se, xmax = coef + 1.96*se, 
      colour = occstatus) + 
  geom_vline(xintercept = 0, lty = 3) + 
  geom_errorbarh(height = 0, position = position_dodgev(height = .5)) + 
  geom_point(position = position_dodgev(height = .5)) + 
  facet_wrap(~attribute, ncol = 1, scales = "free_y") + 
  scale_colour_manual(name = NULL, values = c("White Collar" = "black", "Blue Collar" = "darkblue")) +
  guides(colour = guide_legend(rev = TRUE)) + 
  labs(x = "Average Marginal Component Effect", y = NULL) +
  ggtitle("Effect on Self-Assessed Win Probability, by Occupation Status")
ggsave("figs/prob_conjoint_occstatus.pdf", width=6, height=8)



# check more formally by running full model w/ interactions
prob_interact_occstatus = lm(prob_formula_occstatus, conjoint)
prob_interact_cov = vcovCluster(prob_interact_occstatus, conjoint$responseid)

# test whether there are significant interactions:
int = coeftest(prob_interact_occstatus, vcov. = prob_interact_cov)
cat("\n\nPRINTING OUT TABLE FOR INTERACTIONS BETWEEN TREATMENT AND occstatus, FOR prob OUTCOME:\n\n")
int[!grepl("response", rownames(int)),] 



##### SUBGROUP ANALYSIS: IDEOLOGICAL EXTREMISM ####



conjoint = conjoint %>% mutate(extremism = ifelse(ideo5 %in% c(1, 5), "Extremist", "Non-extremist"))

refcats_notextr = refcats
refcats_notextr$extremism = "Non-extremist"
refcats_extr = refcats
refcats_extr$extremism = "Extremist"
refcats_extremism = rbind(refcats_notextr, refcats_extr)


# create subsets
notextr = subset(conjoint, extremism == "Non-extremist")
extr = subset(conjoint, extremism == "Extremist")




# interactive formula -- get rid of FE b/c they're collinear 
interest_formula_extremism = formula(interest_num ~ extremism : (salary + fundraising + advertising + opp_exp + opp_ideol) +
                                       (salary + fundraising + advertising + opp_exp + opp_ideol) + factor(responseid))
prob_formula_extremism = formula(winprob ~ extremism : (salary + fundraising + advertising + opp_exp + opp_ideol) +
                                   (salary + fundraising + advertising + opp_exp + opp_ideol) + factor(responseid))



# outcome: interest, by extremism --------------------------------------------


# create conjoint plots by running separate regressions

interest_reg_notextr = lm(interest_formula, notextr)
interest_cov_notextr = vcovCluster(interest_reg_notextr, notextr$responseid)
interest_se_notextr = sqrt(diag(interest_cov_notextr))


interest_reg_extr = lm(interest_formula, extr)
interest_cov_extr = vcovCluster(interest_reg_extr, extr$responseid)
interest_se_extr = sqrt(diag(interest_cov_extr))

out_df_interest_notextr = data_frame(variable = names(coef(interest_reg_notextr)[2:10]),
                                     coef = coef(interest_reg_notextr)[2:10],
                                     se = interest_se_notextr[2:10],
                                     extremism = "Non-extremist")

out_df_interest_extr = data_frame(variable = names(coef(interest_reg_extr)[2:10]),
                                  coef = coef(interest_reg_extr)[2:10],
                                  se = interest_se_extr[2:10],
                                  extremism = "Extremist")


out_df_interest_extremism = rbind(out_df_interest_notextr, out_df_interest_extr)

out_df_interest_extremism = rbind(out_df_interest_extremism, refcats_extremism)
out_df_interest_extremism$attribute = NA_character_
out_df_interest_extremism$attribute[grepl("salary", out_df_interest_extremism$variable)] = "Salary"
out_df_interest_extremism$attribute[grepl("fundraising", out_df_interest_extremism$variable)] = "Fundraising"
out_df_interest_extremism$attribute[grepl("advertising", out_df_interest_extremism$variable)] = "Advertising"
out_df_interest_extremism$attribute[grepl("opp_exp", out_df_interest_extremism$variable)] = "Opponent's Experience"
out_df_interest_extremism$attribute[grepl("opp_ideol", out_df_interest_extremism$variable)] = "Opponent's Ideology"

out_df_interest_extremism$variable = gsub("salary|fundraising|advertising|opp_exp|opp_ideol", "", out_df_interest_extremism$variable)

out_df_interest_extremism$variable = factor(out_df_interest_extremism$variable, 
                                            c("$15,000", "$50,000", "$80,000",  # salary
                                              "$25,000", "$100,000", "$300,000", # fundraising
                                              "No negative ads", "Mostly negative ads", # advertising
                                              "No prior experience", "Mayor", "Incumbent", # opponent experience
                                              "Moderate", "Somewhat ideological", "Very ideological")) # opponent ideol
out_df_interest_extremism$attribute = factor(out_df_interest_extremism$attribute, 
                                             c("Salary", "Fundraising", "Advertising", 
                                               "Opponent's Experience", "Opponent's Ideology"))


out_df_interest_extremism$extremism = factor(out_df_interest_extremism$extremism, c("Non-extremist", "Extremist"))


ggplot(out_df_interest_extremism) +
  aes(y = fct_rev(variable), x = coef, xmin = coef - 1.96*se, xmax = coef + 1.96*se, 
      colour = extremism) + 
  geom_vline(xintercept = 0, lty = 3) + 
  geom_errorbarh(height = 0, position = position_dodgev(height = .5)) + 
  geom_point(position = position_dodgev(height = .5)) + 
  facet_wrap(~attribute, ncol = 1, scales = "free_y") + 
  # scale_colour_manual(name = NULL, values = c("Extremist" = "red", "Non-extremist" = "blue")) +
  scale_colour_hc(name=NULL) + 
  guides(colour = guide_legend(rev = TRUE)) + 
  labs(x = "Average Marginal Component Effect", y = NULL) 
  # ggtitle("Effect on Interest in Running,\nby Ideological Extremism")
ggsave("figs/interest_conjoint_extremism.pdf", width=6, height=8)



# check more formally by running full model w/ interactions
interest_interact_extremism = lm(interest_formula_extremism, conjoint)
interest_interact_cov = vcovCluster(interest_interact_extremism, conjoint$responseid)

# test whether there are significant interactions:
int = coeftest(interest_interact_extremism, vcov. = interest_interact_cov)
cat("\n\nPRINTING OUT TABLE FOR INTERACTIONS BETWEEN TREATMENT AND EXTREMISM, FOR INTEREST OUTCOME:\n\n")
int[!grepl("response", rownames(int)),] 






# outcome: win probability, by extremism --------------------------------------


# create conjoint plots by running separate regressions

prob_reg_notextr = lm(prob_formula, notextr)
prob_cov_notextr = vcovCluster(prob_reg_notextr, notextr$responseid)
prob_se_notextr = sqrt(diag(prob_cov_notextr))


prob_reg_extr = lm(prob_formula, extr)
prob_cov_extr = vcovCluster(prob_reg_extr, extr$responseid)
prob_se_extr = sqrt(diag(prob_cov_extr))




out_df_prob_notextr = data_frame(variable = names(coef(prob_reg_notextr)[2:10]),
                                 coef = coef(prob_reg_notextr)[2:10],
                                 se = prob_se_notextr[2:10],
                                 extremism = "Non-extremist")

out_df_prob_extr = data_frame(variable = names(coef(prob_reg_extr)[2:10]),
                              coef = coef(prob_reg_extr)[2:10],
                              se = prob_se_extr[2:10],
                              extremism = "Extremist")


out_df_prob_extremism = rbind(out_df_prob_notextr, out_df_prob_extr)

out_df_prob_extremism = rbind(out_df_prob_extremism, refcats_extremism)
out_df_prob_extremism$attribute = NA_character_
out_df_prob_extremism$attribute[grepl("salary", out_df_prob_extremism$variable)] = "Salary"
out_df_prob_extremism$attribute[grepl("fundraising", out_df_prob_extremism$variable)] = "Fundraising"
out_df_prob_extremism$attribute[grepl("advertising", out_df_prob_extremism$variable)] = "Advertising"
out_df_prob_extremism$attribute[grepl("opp_exp", out_df_prob_extremism$variable)] = "Opponent's Experience"
out_df_prob_extremism$attribute[grepl("opp_ideol", out_df_prob_extremism$variable)] = "Opponent's Ideology"

out_df_prob_extremism$variable = gsub("salary|fundraising|advertising|opp_exp|opp_ideol", "", out_df_prob_extremism$variable)

out_df_prob_extremism$variable = factor(out_df_prob_extremism$variable, 
                                        c("$15,000", "$50,000", "$80,000",  # salary
                                          "$25,000", "$100,000", "$300,000", # fundraising
                                          "No negative ads", "Mostly negative ads", # advertising
                                          "No prior experience", "Mayor", "Incumbent", # opponent experience
                                          "Moderate", "Somewhat ideological", "Very ideological")) # opponent ideol
out_df_prob_extremism$attribute = factor(out_df_prob_extremism$attribute, 
                                         c("Salary", "Fundraising", "Advertising", 
                                           "Opponent's Experience", "Opponent's Ideology"))


out_df_prob_extremism$extremism = factor(out_df_prob_extremism$extremism, c("Non-extremist", "Extremist"))


ggplot(out_df_prob_extremism) +
  aes(y = fct_rev(variable), x = coef, xmin = coef - 1.96*se, xmax = coef + 1.96*se, 
      colour = extremism) + 
  geom_vline(xintercept = 0, lty = 3) + 
  geom_errorbarh(height = 0, position = position_dodgev(height = .5)) + 
  geom_point(position = position_dodgev(height = .5)) + 
  facet_wrap(~attribute, ncol = 1, scales = "free_y") + 
  scale_colour_hc(name = "Ideological\nExtremism") + 
  guides(colour = guide_legend(rev = TRUE)) + 
  labs(x = "Average Marginal Component Effect", y = NULL) 
  # ggtitle("Effect on Self-Assessed Win Probability, by Extremism")
ggsave("figs/prob_conjoint_extremism.pdf", width=6, height=8)



# check more formally by running full model w/ interactions
prob_interact_extremism = lm(prob_formula_extremism, conjoint)
prob_interact_cov = vcovCluster(prob_interact_extremism, conjoint$responseid)

# test whether there are significant interactions:
int = coeftest(prob_interact_extremism, vcov. = prob_interact_cov)
cat("\n\nPRINTING OUT TABLE FOR INTERACTIONS BETWEEN TREATMENT AND extremism, FOR prob OUTCOME:\n\n")
int[!grepl("response", rownames(int)),] 











##### DISTRIBUTIONS OF DV #####

# make tables for the distribution of the DV's
interestrecode = "-2 = 'Not interested'; -1 = 'Slightly interested'; 0 = 'Somewhat interested'; 1 = 'Moderately interested'; 2 = 'Very interested'"
conjoint %>% 
  mutate(int.text = car::recode(interest, interestrecode)) %>% 
  mutate(int.text = factor(int.text, c("Not interested", 
                                       "Slightly interested",
                                       "Somewhat interested",
                                       "Moderately interested",
                                       "Very interested"))) %>% 
  filter(!is.na(int.text)) %>% 
  group_by(int.text) %>% 
  summarise(n = n()) %>% 
  ungroup() %>% 
  mutate(prop = paste0(round(n / sum(n) * 100,1), "%")) %>% 
  rename(`N` = n, Percent = prop, `Interest in running` = int.text) %>% 
  xtable() %>% 
  print(floating = FALSE, 
        include.rownames = FALSE,
        file = "docs/tables/interest_distribution.tex")

#
probrecode = "5 = 'Very unlikely'; 25 = 'Moderately unlikely'; 50 = '50/50'; 75 = 'Moderately likely'; 95 = 'Very likely'"
conjoint %>% 
  mutate(prob.text = car::recode(winprob, probrecode)) %>% 
  mutate(prob.text = factor(prob.text, c("Very unlikely", 
                                         "Moderately unlikely",
                                         "50/50",
                                         "Moderately likely",
                                         "Very likely"))) %>% 
  filter(!is.na(prob.text)) %>% 
  group_by(prob.text) %>% 
  summarise(n = n()) %>% 
  ungroup() %>% 
  mutate(prop = paste0(round(n / sum(n) * 100,1), "%")) %>% 
  rename(`N` = n, Percent = prop, `Win probability` = prob.text) %>% 
  xtable() %>% 
  print(floating = FALSE, 
        include.rownames = FALSE,
        file = "docs/tables/prob_distribution.tex")

